﻿using System;
using System.Text;
using System.Web;

namespace Rison.Web.Common
{
    public sealed class PageGlobalParams
    {
        private PageGlobalParams()
        {
        }


        /// <summary>
        /// 应用程序运行的跟http地址。
        /// 以一个会话访问（Session）为单位来记录。
        /// 可避免若系统允许同一用户在不同地方同时登录时可能造成的地址互串问题。
        /// </summary>
        public static string PageUrlBase
        {
            get
            {
                if (HttpContext.Current != null)
                {
                    if (HttpContext.Current.Session["CurrentUserPageUrlBase"] == null ||
                        HttpContext.Current.Session["CurrentUserPageUrlBase"].ToString().Trim().Length == 0)
                    {
                        lock (typeof(PageGlobalParams))//
                        {
                            if (HttpContext.Current.Session["CurrentUserPageUrlBase"] == null ||
                                HttpContext.Current.Session["CurrentUserPageUrlBase"].ToString().Trim().Length == 0)
                            {
                                StringBuilder url = new StringBuilder();

                                if (HttpContext.Current.Request.IsSecureConnection)
                                    url.Append("https://");
                                else
                                    url.Append("http://");

                                url.Append(HttpContext.Current.Request.Url.Host);

                                if (!HttpContext.Current.Request.Url.IsDefaultPort)
                                    url.AppendFormat(":{0}", HttpContext.Current.Request.Url.Port.ToString());

                                if (!"/".Equals(HttpContext.Current.Request.ApplicationPath))
                                    url.Append(HttpContext.Current.Request.ApplicationPath);

                                if (!url.ToString().EndsWith("/"))
                                    url.Append("/");

                                HttpContext.Current.Session.Add("CurrentUserPageUrlBase", url.ToString());
                            }
                        }
                    }
                }

                if (HttpContext.Current.Session["CurrentUserPageUrlBase"] != null)
                    return HttpContext.Current.Session["CurrentUserPageUrlBase"].ToString();

                return string.Empty;
            }
        }

        /// <summary>
        /// 去除最后一个“/”的地址
        /// </summary>
        public static string PageUrlBase2
        {
            get { return PageUrlBase.Substring(0, PageUrlBase.Length - 1); }
        }

        public static string CssUrlBase
        {
            get
            {
                return PageUrlBase + "style/css/";
            }
        }
    }
}
